package com.fuyao.cloud.personnel.problem.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.DictItemOption;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.mybatis.SpliceQueryWrapper;
import com.fuyao.cloud.common.data.resolver.DictItemResolver;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.personnel.api.entity.TenderSupervision;
import com.fuyao.cloud.personnel.api.vo.TenderSuperviSearchVO;
import com.fuyao.cloud.personnel.api.vo.TenderSupperviBasicVO;
import com.fuyao.cloud.personnel.api.vo.excel.NonPubTenderExcelVO;
import com.fuyao.cloud.personnel.api.vo.excel.PubTenderExcelVO;
import com.fuyao.cloud.personnel.api.vo.excel.TenderSupervisionExeclVO;
import com.fuyao.cloud.personnel.problem.service.TenderSupervisionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 招标表
 *
 * @author hyc
 * @date 2022-05-20 10:52:12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/tendersupervision" )
@Api(value = "tendersupervision", tags = "招标表管理")
public class TenderSupervisionController {

    private final TenderSupervisionService tenderSupervisionService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param tenderSupervision 招标表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('tender_supervision_view')" )
    public R getFyTenderSupervisionPage(Page page, TenderSuperviSearchVO tenderSupervision) {
        return R.ok(tenderSupervisionService.page(page, SpliceQueryWrapper.spliceSearchCondition(tenderSupervision)));
    }


    /**
     * 通过id查询招标表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('tender_supervision_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(tenderSupervisionService.getById(id));
    }

    /**
     * 新增招标表
     * @param tenderSupervision 招标表
     * @return R
     */
    @ApiOperation(value = "新增招标表", notes = "新增招标表")
    @SysLog("新增招标表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('tender_supervision_add')")
    public R save(@RequestBody TenderSupervision tenderSupervision) {
        return R.ok(tenderSupervisionService.save(tenderSupervision));
    }

    /**
     * 新增招标表
     * @param tenderSupperviBasicVO 招标表
     * @return R
     */
    @ApiOperation(value = "新增招标表", notes = "新增招标表")
    @SysLog("新增招标表" )
    @PostMapping("/saveAndBasic")
    @PreAuthorize("@pms.hasPermission('tender_supervision_add')" )
    public R saveAndBasic(@RequestBody TenderSupperviBasicVO tenderSupperviBasicVO) {
        return R.ok(tenderSupervisionService.saveTenderSupperviBasicVO(tenderSupperviBasicVO));
    }

    /**
     * 修改招标表
     * @param tenderSupervision 招标表
     * @return R
     */
    @ApiOperation(value = "修改招标表", notes = "修改招标表")
    @SysLog("修改招标表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('tender_supervision_edit')" )
    public R updateById(@RequestBody TenderSupervision tenderSupervision) {
        return R.ok(tenderSupervisionService.updateById(tenderSupervision));
    }

    /**
     * 通过id删除招标表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除招标表", notes = "通过id删除招标表")
    @SysLog("通过id删除招标表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('tender_supervision_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(tenderSupervisionService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param fyTenderSupervision 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @PostMapping("/export")
    @PreAuthorize("@pms.hasPermission('tender_supervision_export')" )
    public List<TenderSupervisionExeclVO> export(TenderSupervision fyTenderSupervision) {
        List<TenderSupervision> supervisionList = tenderSupervisionService.list(Wrappers.query(fyTenderSupervision));
        List<TenderSupervisionExeclVO> exclVOList = Lists.newArrayList();
        if(CollUtil.isNotEmpty(supervisionList)){
            Map<String, List<DictItemOption>> dictOptionResultMap = DictItemResolver.getDictOptionResultMap(new String[]{TenderSupervision.TENDER_METHOD, TenderSupervision.EVALUATION_PRINCIPLE});
            supervisionList.forEach(tenderSupervision -> {
                TenderSupervisionExeclVO exclVO = new TenderSupervisionExeclVO();
                BeanUtil.copyProperties(tenderSupervision,exclVO);
                List<DictItemOption> dictItemOptions = dictOptionResultMap.get(TenderSupervision.TENDER_METHOD);
                exclVO.setTenderMethod(DictItemResolver.getDictOptionLabel(dictItemOptions, tenderSupervision.getTenderMethod()));
                exclVO.setEvaluationPrinciple(DictItemResolver.getDictOptionLabel(dictOptionResultMap.get(TenderSupervision.EVALUATION_PRINCIPLE), String.valueOf(tenderSupervision.getEvaluationPrinciple())));
                exclVOList.add(exclVO);
            });
        }
        return exclVOList;
    }

    /**
     * 导入公共投标
     * @return R
     */
    @PostMapping("/pubTenderImport")
    @PreAuthorize("@pms.hasPermission('tender_supervision_export')")
    public R importPubTender(@RequestExcel List<PubTenderExcelVO> excelVOList, BindingResult bindingResult) {
        return tenderSupervisionService.importPubTender(excelVOList, bindingResult);
    }

    /**
     * 导入非公共投标
     * @return R
     */
    @PostMapping("/nonPubTenderImport")    @PreAuthorize("@pms.hasPermission('tender_supervision_export')")
    public R importNonPubTender(@RequestExcel List<NonPubTenderExcelVO> excelVOList, BindingResult bindingResult) {
        return tenderSupervisionService.importNonPubTender(excelVOList, bindingResult);
    }

}
